package com.jiesone.logger.distributecleaning.util;
/*
 *@ClassName JsonStringUtil
 *@Description TODO
 *@Author tangsixiang@163.com
 *@Date 2022/1/18 11:57
 *@Version 1.0
 */

import com.alibaba.fastjson.JSONObject;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class JsonStringUtil {


    /**
     * 判断是否符合JSON要求字符串
     * @param value
     * @return
     */
    public  static    boolean  isCheckJSON(String value) {
        try {
            boolean result = false;
            String jsonRegexp = "^(?:(?:\\s*\\[\\s*(?:(?:"
                    + "(?:\"[^\"]*?\")|(?:true|false|null)|(?:[+-]?\\d+(?:\\.?\\d+)?(?:[eE][+-]?\\d+)?)|(?<json1>(?:\\[.*?\\])|(?:\\{.*?\\})))\\s*,\\s*)*(?:"
                    + "(?:\"[^\"]*?\")|(?:true|false|null)|(?:[+-]?\\d+(?:\\.?\\d+)?(?:[eE][+-]?\\d+)?)|(?<json2>(?:\\[.*?\\])|(?:\\{.*?\\})))\\s*\\]\\s*)"
                    + "|(?:\\s*\\{\\s*"
                    + "(?:\"[^\"]*?\"\\s*:\\s*(?:(?:\"[^\"]*?\")|(?:true|false|null)|(?:[+-]?\\d+(?:\\.?\\d+)?(?:[eE][+-]?\\d+)?)|(?<json3>(?:\\[.*?\\])|(?:\\{.*?\\})))\\s*,\\s*)*"
                    + "(?:\"[^\"]*?\"\\s*:\\s*(?:(?:\"[^\"]*?\")|(?:true|false|null)|(?:[+-]?\\d+(?:\\.?\\d+)?(?:[eE][+-]?\\d+)?)|(?<json4>(?:\\[.*?\\])|(?:\\{.*?\\}))))\\s*\\}\\s*))$";

            Pattern jsonPattern = Pattern.compile(jsonRegexp);

            Matcher jsonMatcher = jsonPattern.matcher(value);

            if (jsonMatcher.matches()) {

                result = true;
                for (int i = 4; i >= 1; i--) {
                    if (!StringUtils.isEmpty(jsonMatcher.group("json" + i))) {
                        result =  isCheckJSON(jsonMatcher.group("json" + i));
                        if (!result) {
                            break;
                        }
                        if (i == 3 || i == 1) {
                            result =  isCheckJSON(value.substring(0, jsonMatcher.start("json" + i))
                                    + (i == 3 ? "\"JSON\"}" : "\"JSON\"]"));
                            if (!result) {
                                break;
                            }
                        }
                    }
                }

            }
            return result;
        } catch (Exception e) {
            return false;
        }
    }



    public static boolean isJson(String content) {
        if(StringUtils.isEmpty(content)){
            return false;
        }
        boolean isJsonObject = true;
        boolean isJsonArray = true;
        try {
            JSONObject.parseObject(content);
        } catch (Exception e) {
            isJsonObject = false;
        }
        try {
            JSONObject.parseArray(content);
        } catch (Exception e) {
            isJsonArray = false;
        }
        if(!isJsonObject && !isJsonArray){ //不是json格式
            return false;
        }
        return true;
    }
    public static boolean getJSONType(String str) {

        boolean result = false;

        if (!com.mysql.jdbc.StringUtils.isNullOrEmpty(str)) {

            str = str.trim();

            if (str.startsWith("{") && str.endsWith("}")) {

                result = true;

            } else if (str.startsWith("[") && str.endsWith("]")) {

                result = true;

            }

        }

        return result;

    }



    public  static String   makeJsontoString(String str) {

         if(str!=null&&!str.equals("") ){

             if(str.indexOf("{")>=0&&str.indexOf("}")>=1){

                 if(str.indexOf("{")<str.indexOf("[")){
                     str= str.substring(str.indexOf("{"),str.lastIndexOf("}"));
                 }else{
                     str= str.substring(str.indexOf("["),str.lastIndexOf("]"));
                 }

             }
              }

        return str;

    }

    public  static  String  checkStrForValue(String  indexName,Object  obj ){


         if(obj!=null){
             if(obj.equals("H")){
                 return    indexName+ DateUtil.getDateByString("yyyy-MM-dd-HH");
             }else if(obj.equals("D")){
                 return   indexName+ DateUtil.getDateByString("yyyy-MM-dd");
             }
             else if(obj.equals("M")){
                 return   indexName+  DateUtil.getDateByString("yyyy-MM");
             }  else if(obj.equals("Y")){
                 return   indexName+  DateUtil.getDateByString("yyyy");
             }
         }

         return indexName;


    }


    /**
     *判断 目标字符串包含的日志等级
     * @param targetData
     * @return
     */
    public static   String  checkLoggerLevel(String targetData){
     String[] checkStr= {" ERROR "," WARN "," INFO "," DEBUG "," FATAL ","ERROR","WARN","INFO","FATAL"};

       for (String str:checkStr){
           if(targetData.indexOf(str)>=0){
               return str.trim();
           }

       }





         return  null;
    }

    /**
     *
     * @param dbLevel
     * @param checkLeval
     * @return
     */
    public static   boolean  checkLevel(String dbLevel,String checkLeval){

       Map<String,Integer> map=new HashMap<>();
        map.put("NOR",0);
          map.put("DEBUG",1);
        map.put("INFO",2);  map.put("WARN",3);map.put("ERROR",4);map.put("TATAL",5);map.put("OFF",6);map.put("ALL",7);

        if(dbLevel!=null&&checkLeval!=null){

            if(map.getOrDefault(checkLeval.toUpperCase(),4)>=Integer.parseInt(dbLevel)){
                return true;
            }
        }
        return  false;

    }




}
